package cn.sofwin.core.utils.security.jd;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import cn.sofwin.core.utils.common.StringUtils;

import java.security.SecureRandom;

/**
 * DES通用类
 *
 * @version 1.0.0_1
 * @since 2013.01.15
 */
public class DES {
	
	public static String CHARSET = "UTF-8";
	
    /**
     * 生成密钥方法
     *
     * @param seed 密钥种子
     * @return 密钥 BASE64
     * @throws Exception
     */
    public static String generateKey(String seed) throws Exception {
        byte[] seedBase64DecodeBytes = StringUtils.getBytesBase64(seed);

        SecureRandom secureRandom = new SecureRandom(seedBase64DecodeBytes);
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(secureRandom);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytes = secretKey.getEncoded();

        return StringUtils.getStringBase64(bytes);
    }

    /**
     * 加密方法
     *
     * @param text    明文
     * @param key     密钥 BASE64
     * @param CHARSET 字符集
     * @return 密文
     * @throws Exception
     */
    public static String encrypt(String text, String key){
    	String encryptBase64EncodeString = null;
    	 
        try {
			byte[] keyBase64DecodeBytes = StringUtils.getBytesBase64(key);//base64解码key
			DESKeySpec desKeySpec = new DESKeySpec(keyBase64DecodeBytes);//前8个字节做为密钥
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			byte[] textBytes = text.getBytes(CHARSET);//明文UTF-8格式
			byte[] bytes = cipher.doFinal(textBytes);//DES加密

			encryptBase64EncodeString = StringUtils.getStringBase64(bytes);//base64编码
		} catch (Exception e) {
			e.printStackTrace();
		}

        return encryptBase64EncodeString;
    }

    /**
     *
     * @param text 明文
     * @param key 8字节秘钥
     * @param CHARSET 字符集
     * @return
     * @throws Exception
     */
    public static String encrypt(String text, byte[] key) {
    	String encryptBase64EncodeString = null;
    	
        try {
			DESKeySpec desKeySpec = new DESKeySpec(key);//前8个字节做为密钥
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			byte[] textBytes = text.getBytes(CHARSET);//明文UTF-8格式
			byte[] bytes = cipher.doFinal(textBytes);//DES加密

			encryptBase64EncodeString = StringUtils.getStringBase64(bytes);//base64编码
		} catch (Exception e) {
			e.printStackTrace();
		}

        return encryptBase64EncodeString;
    }

    /**
     * 解密方法
     *
     * @param text    密文
     * @param key     密钥 BASE64
     * @param CHARSET 字符集
     * @return 明文
     * @throws Exception
     */
    public static String decrypt(String text, String key) {
    	String decryptString = null;
    	
        try {
			byte[] keyBase64DecodeBytes = StringUtils.getBytesBase64(key);

			DESKeySpec desKeySpec = new DESKeySpec(keyBase64DecodeBytes);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			byte[] textBytes = StringUtils.getBytesBase64(text);
			byte[] bytes = cipher.doFinal(textBytes);

			decryptString = new String(bytes, CHARSET);
		} catch (Exception e) {
			e.printStackTrace();
		}

        return decryptString;
    }

    /**
     * @param text    密文
     * @param key     8字节秘钥
     * @param CHARSET 字符集
     * @return
     * @throws Exception
     */
    public static String decrypt(String text, byte[] key) {
    	String decryptString = null;

        try {
			DESKeySpec desKeySpec = new DESKeySpec(key);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			byte[] textBytes = StringUtils.getBytesBase64(text);
			byte[] bytes = cipher.doFinal(textBytes);

			decryptString = new String(bytes, CHARSET);
		} catch (Exception e) {
			e.printStackTrace();
		}

        return decryptString;
    }


}